#!/usr/bin/python
# -*- coding: utf-8 -*-

import urllib
import re
from app.lib import BaseHandler
from web.session import Session
from model.user import User
from model.user import UserManager
from igor import logging
from utils.common import check_login 
from utils.common import set_session 
from utils.common import render 
from utils.model import md5 
from igor import logging

class Login(BaseHandler):

    @check_login
    def get(self, req, resp):  
        body = render('login.html') 
        return resp.respond(body)

    def post(self, req, resp):
        username = req.args.get('username', [''])[0]
        passwd = req.args.get('passwd', [''])[0]
        if username != '' and passwd != '':
            user = User()
            user.username, user.passwd = username, passwd
            umanager = UserManager() 
            success = umanager.auth(user)
            if success:
                user = umanager.query(username)
                set_session(req, resp, user)
                body = 'True'
            else:
                body = 'False'
        else:
            body = 'False'
        return resp.respond(body)


class Logout(BaseHandler):

    def get(self, req, resp):  
        session = Session(req)
        session.invalidate()
        resp.clear_cookie('sid')
        return resp.redirect('/')


class Signup(BaseHandler):

    @check_login
    def get(self, req, resp):  
        body = render('signup.html') 
        return resp.respond(body)

    def post(self, req, resp):
        args = req.args
        username = args.get('username', [''])[0]
        passwd = args.get('passwd', [''])[0]
        checkpasswd = args.get('checkpasswd', [''])[0]
        name = args.get('name', [''])[0]
        birth = args.get('birth', [''])[0]
        tel = args.get('tel', [''])[0]
        addr = args.get('addr', [''])[0]
        postcode = args.get('postcode', [''])[0]
        
        logging.log('''args username=%s, passwd=%s, checkpasswd=%s, name=%s, 
                birth=%s, tel=%s, addr=%s, postcode=%s''' % (
            username, passwd, checkpasswd, name, birth, tel, addr, postcode))
        if not self.__validate(username, passwd, checkpasswd, tel, postcode):
            logging.log('validate false')
            body = 'False'
        else:
            name = urllib.unquote(name)
            addr = urllib.unquote(addr)
            user = User(username, passwd, name, birth, tel, addr, postcode)
            umanager = UserManager()
            succ = umanager.signup(user)
            if succ:
                user = umanager.query(username)
                set_session(req, resp, user)
                body = 'True' 
            else:
                body = 'False' 
        return resp.respond(body)

    def __validate(self, username, passwd, checkpasswd, tel, postcode):
        p_username = re.compile(r'^\w{5,30}$')
        p_tel = re.compile(r'^0*(13|15|18)\d{9}$')
        p_postcode = re.compile(r'^\d{6}$')
        if username == '' or not p_username.match(username):
            return False
        if passwd == '' or checkpasswd == '' or passwd != checkpasswd:
            return False
        if tel != '' and not p_tel.match(tel):
            return False
        if postcode != '' and not p_postcode.match(postcode):
            return False
        return True
        

